package com.uade.test;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.uade.facade.Facade;

public class ServiceLocator {
	public static Facade getRemoteFacade() throws NamingException {
		final String appName = PropertiesEnum.APP_NAME.toString();
		final String moduleName = PropertiesEnum.MODULE_NAME.toString();
		final String sessionBeanName = PropertiesEnum.SESSION_BEAN_NAME
				.toString();
		final String viewClassName = Facade.class.getName();
		Properties jndiProps = new Properties();
		jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
				"org.jboss.naming.remote.client.InitialContextFactory");
		jndiProps.put(Context.PROVIDER_URL,
				PropertiesEnum.PROVIDER_URL.toString());
		// username
		jndiProps.put(Context.SECURITY_PRINCIPAL, PropertiesEnum.SECURITY_PRINCIPAL.toString());
		// password
		jndiProps.put(Context.SECURITY_CREDENTIALS, PropertiesEnum.SECURITY_CREDENTIALS.toString());
		// This is an important property to set if you want to do EJB
		// invocations via the remote naming project
		jndiProps.put("jboss.naming.client.ejb.context", true);
		// create context passing these properties
		Context context = new InitialContext(jndiProps);

		// lookup the bean facade
		Facade facade = (Facade) context.lookup(appName + "/" + moduleName
				+ "/" + sessionBeanName + "!" + viewClassName);

		return facade;
	}

	private enum PropertiesEnum {
		APP_NAME("PortalBackendEar"), MODULE_NAME("PortalBackendEarEJB"), SESSION_BEAN_NAME(
				"FacadeBean"), PROVIDER_URL("remote://127.0.0.1:4447"), SECURITY_PRINCIPAL(
				"test"), SECURITY_CREDENTIALS("test123");

		private PropertiesEnum(final String text) {
			this.text = text;
		}

		private final String text;

		@Override
		public String toString() {
			return text;
		}
	}
}
